package org.bizming.record.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.bizming.base.service.SqlHelper;
import org.bizming.record.model.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class RecordDaoMysql implements RecordDao {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	private JdbcTemplate jdbcTemplate;

	private SqlHelper sqlHelper;

	private String tableName = "record";

	public boolean add(Record record) {
		List<Object> values = new ArrayList<Object>();
		String sql = sqlHelper.sqlAdd(record, values, getTableName());
		logger.info(sql);

		Object[] o = values.toArray();
		int affectRow = jdbcTemplate.update(sql, o);

		sqlHelper.clear(values);
		sqlHelper.clear(o);

		if (affectRow > 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean update(Record record) {
		List<Object> values = new ArrayList<Object>();
		String sql = sqlHelper.sqlUpdate(record, values, getTableName());
		logger.info(sql);

		values.add(record.getId());
		Object[] o = values.toArray();
		int affectRow = jdbcTemplate.update(sql, o);

		sqlHelper.clear(values);
		sqlHelper.clear(o);

		if (affectRow > 0) {
			return true;
		} else {
			return false;
		}
	}

	public Record get(int id) {

		String sql = sqlHelper.sqlGet(null, getTableName());
		logger.info(sql);

		Record record = jdbcTemplate.queryForObject(sql, new RecordRowMapper(),
				id);

		return record;
	}

	public List<Record> selectList(int limit, int page) {

		String sql = sqlHelper.sqlSelectList(Record.class, getTableName());
		logger.info(sql);

		Object[] args = new Object[] { limit, page };

		List<Record> rows = jdbcTemplate
				.query(sql, new RecordRowMapper(), args);

		sqlHelper.clear(args);

		return rows;
	}

	public int count() {
		String sql = sqlHelper.sqlCount(getTableName());
		logger.info(sql);

		int row = jdbcTemplate.queryForInt(sql);
		return row;
	}

	class RecordRowMapper implements RowMapper<Record> {

		public Record mapRow(ResultSet rs, int rowNum) throws SQLException {
			Record record = new Record();
			record.setId(rs.getInt("id"));
			record.setUsername(rs.getString("username"));
			record.setCreateTime(rs.getLong("createTime"));
			record.setActionTime(rs.getLong("actionTime"));
			record.setCost(rs.getDouble("cost"));
			record.setTypeId(rs.getInt("typeId"));
			record.setMark(rs.getString("mark"));
			record.setDescription(rs.getString("description"));
			return record;
		}

	}

	// =================================
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public SqlHelper getSqlHelper() {
		return sqlHelper;
	}

	public void setSqlHelper(SqlHelper sqlHelper) {
		this.sqlHelper = sqlHelper;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

}
